/**
 * 
 */
package com.gmail.sabujdas.patterns.singleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class IOSafeLazySingleton implements Serializable{

	private static volatile int count = 0;
	private static IOSafeLazySingleton instance;
	private int x;
	
	private IOSafeLazySingleton(){
		count ++;
		System.out.println("Instance Count = " + count);
		x = 10;
	}

	/**
	 * @return the x
	 */
	public int getX() {
		return x;
	}

	/**
	 * @param x the x to set
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * @return the instance
	 */
	public static IOSafeLazySingleton getInstance() {
		if(instance == null){
			synchronized (ThreadSafeLazySingleton.class) {
				if(instance == null)
					instance = new IOSafeLazySingleton();
			}
		}
		return instance;
	}

	
	
	
	private Object readResolve() throws ObjectStreamException{
		instance = getInstance();
		return instance;
	}
}
